$(function () {

    
    function getList() {
        $.ajax({
            method: 'get',
            url: 'http://150.158.137.166:3001/wish/list',
            success: function (res) {
                if (res.status !== 200) {
                    console.log('获取数据失败');
                }
                const rows = []
                res.data.forEach(obj => {
                    console.log(obj)
                    const htmlstr =`

                    <div class="item" style='background:${obj.color}'>
                      <div class="tit">
                        <span>From：<b>${obj.username}</b></span>
                        <span class="iconfont icon-shanchu"  data-id ="${obj.id}"></span>
                      </div>
                      <div class="content">
                      ${obj.content}
                        <br />
                        ${obj.time}
                      </div>
                    </div>
                    `
                    rows.push(htmlstr)
                });
                $('.list-box').html(rows.join(''))
            }
        })
    }
    getList()
    
    function postlist() {
        $.ajax({
            method: 'post',
            url: 'http://150.158.137.166:3001/wish/add',
            data: {
                username: $("#username").val(),
                color:$('.color-list .active').attr('data-color'),
                content:$('#wish').val(),
                
            },
            success: function (res) {
                if (res.status !== 200) {
                   return   alert('上传不正确');
                }
              
                
                getList()
              
            }
        })
    }

    $(' .add').click(function () {
        //alert('ok')
        $('.alert-box').show()
    })
    
    $('.cancel').click(function () {
       // alert('ok')
        $('#username').val('')
        $('#wish').val('')
        $('.alert-box').hide()
    })

  
    $('.color-list li').click(function () {
        $(this).addClass("active").siblings().removeClass("active");
       var index = $(this).index();
        console.log(index);
    
    })


    $('.btn-box .ok').click(function () {
       // alert('ok')
        postlist()
        $('#username').val('')
        $('#wish').val('')
        $('.alert-box').hide()
    })
    
 
    $('.list-box').on('click','.icon-shanchu',function () {
        
        alert('ok')
        let id = $(this).attr('data-id')
        $.ajax({
            type: 'delete',
            url: 'http://150.158.137.166:3001/wish/del/'+id,
            success: function (res) {
                console.log(res);
                if (res.status === 200) {
                    console.log('删除数据成功');
                    getList()
                }
            }
        })
    })
    
})